package com.coolgor.coldot.web.auth;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.coolgor.xutil.Net.HttpServletRequestUtil;
import com.coolgor.coldot.dto.auth.FunctionExecution;
import com.coolgor.coldot.dto.auth.LocalUserExecution;
import com.coolgor.coldot.entity.auth.Function;
import com.coolgor.coldot.entity.auth.LocalUser;
import com.coolgor.coldot.entity.auth.Role;
import com.coolgor.coldot.enums.auth.FunctionStateEnum;
import com.coolgor.coldot.enums.auth.LocalUserOperationStateEnum;
import com.coolgor.coldot.service.auth.FunctionService;
import com.coolgor.coldot.service.auth.LocalUserService;


@Controller
@RequestMapping(value = "auth", method = { RequestMethod.GET, RequestMethod.POST })
public class AuthController {
	@Autowired
	private FunctionService functionService;
	@Autowired
	private LocalUserService localUserService;
	
	@RequestMapping(value = "/getLoginUser", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getLoginUser(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		LocalUser loginUser = (LocalUser) request.getSession().getAttribute("login_user");
		if (null != loginUser) {
			try {
				modelMap.put("login_user", loginUser);
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty loginUser");
		}
		return modelMap;
	}	
	
	@RequestMapping(value = "/getLoginRole", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getLoginRole(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Role loginRole = (Role) request.getSession().getAttribute("login_role");
		if (null != loginRole) {
			try {
				modelMap.put("login_role", loginRole);
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty loginRole");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getLoginUserRoleList", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getLoginUserRoleList(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		@SuppressWarnings("unchecked")
		List<Role> roleList = (List<Role>) request.getSession().getAttribute("role_list");
		if (null != roleList) {
			try {
				modelMap.put("role_list", roleList);
				modelMap.put("success", true);
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty login_role_list");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/getLoginUserFunctionTree", method = RequestMethod.GET)
	@ResponseBody
	private Map<String, Object> getLoginUserFunctionTree(HttpServletRequest request) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		Role loginRole = (Role) request.getSession().getAttribute("login_role");
		if (null != loginRole) {
			try {
				Function function = new Function();
				//老法师信息平台的根节点ID为2,先赋值2为根节点的FunctionID。
				function.setFunctionID(2L);
				FunctionExecution functionEx = functionService.getFunctionTreeByRole(function, loginRole);
				if (functionEx.getState()==FunctionStateEnum.SUCCESS.getState()) {
					modelMap.put("function_tree", functionEx.getFunctionTree());
					modelMap.put("success", true);
				}
			} catch (Exception e) {
				modelMap.put("success", false);
				modelMap.put("errMsg", e.toString());
			}
		} else {
			modelMap.put("success", false);
			modelMap.put("errMsg", "empty function tree");
		}
		return modelMap;
	}
	
	@RequestMapping(value = "/checkLoginUserPassword", method = RequestMethod.GET)
	@ResponseBody
	private boolean checkLoginUserPassword(HttpServletRequest request) {
		LocalUser loginUser = (LocalUser) request.getSession().getAttribute("login_user");
		String password = request.getParameter("password");
		if (null != password && !"".equals(password)) {
			if (loginUser.getPassword().equals(password)) {
				return true;
			} else {
				return false;
			}
		} else {
			return false;
		}
	}
	
	@RequestMapping(value = "/modifyLoginUserPassword", method = RequestMethod.POST)
	@ResponseBody
	private boolean modifyLoginUserPassword(HttpServletRequest request) {
		LocalUser loginUser = (LocalUser) request.getSession().getAttribute("login_user");
		String oldPassword = HttpServletRequestUtil.getString(request, "oldpassword"); 
		if ( null == oldPassword ) return false ;
		String newPassword = HttpServletRequestUtil.getString(request, "newpassword");
		if ( null == newPassword ) return false ;
		//判断原密码是否正确, 若原密码错误，直接返回失败。
		if (!loginUser.getPassword().equals(oldPassword)) return false;
		loginUser.setPassword(newPassword);
		LocalUserExecution exe = localUserService.modifyLocalUser(loginUser);
		if ( exe.getState() == LocalUserOperationStateEnum.SUCCESS.getState() ) {
			request.getSession().setAttribute("login_user",loginUser);
			return true;
		} else {
			return false;
		}		
	}
	
}
